<script setup lang="ts">

import { ref } from 'vue'
import { onMounted } from 'vue'
import { apiUserLogin } from '@/api/api'
import type { RespUser } from '@/api/apiUser'
import { validEmail } from "@/utils/validate"
import { SaveUser ,SaveToken } from "@/utils/lStorage"
import router from '@/router'
import{apiUserList,apiUserDetail,apiUserUpdate, apiUserResetPW} from "@/api/api"

import ComponentFooter from "@/components/footer.vue"




const loginParams = ref({
  username:"",
  pw:"",
  userId : 0, 
})


const msg = ref("")
const flagLoginFailed = ref(false)

async function Login() {
  if (loginParams.value.username == "") {
    flagLoginFailed.value = true
    msg.value = "请输入邮箱地址"
    return
  }

  if (loginParams.value.pw == "") {
    flagLoginFailed.value = true
    msg.value = "请输入密码"
    return
  }

  const resp = await apiUserLogin(loginParams.value.username, loginParams.value.pw)
  if (resp.CODE != "ok") {
    console.log(resp)
    msg.value = "用户名或密码不正确"
    flagLoginFailed.value = true
    return
  }
  flagLoginFailed.value = false
  SaveUser(resp.DATA)



  router.push({ path: '/group/list' })

}




function cleanFlag() {
  flagLoginFailed.value = false
}

</script>

<template>
  <div class="tbody">
    <div class="container">
      <div class="form">

        <div class="form-control">
          <label for="username">用户名</label>
          <el-input @focus="cleanFlag" size="large" type="text" v-model="loginParams.username" placeholder="请输入用户名" clearable />

        </div>


        <div class="form-control">
          <label for="password">密码</label>
          <el-input @focus="cleanFlag" size="large" type="password" v-model="loginParams.pw" placeholder="请输入密码" show-password clearable />

        </div>

        <small v-show="flagLoginFailed" class="errorMsg">{{ msg }}</small>
        <button @click="Login">登录</button>

      </div>
    </div>
  </div>

  <ComponentFooter />

</template>

<style scoped>
.tbody {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 95vh;
  margin: 0;
}

.container {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  width: 400px;
}



.form {
  padding: 30px 40px;
}

.form-control {
  margin-bottom: 10px;
  padding-bottom: 20px;
  position: relative;
}

.form-control label {
  color: #777;
  display: block;
  margin-bottom: 5px;
}

.errorMsg {
  color: #e74c3c;
  bottom: 0;
  left: 0;
}


.form button {
  cursor: pointer;
  background-color: #3498db;
  border: 2px solid #3498db;
  border-radius: 4px;
  color: #fff;
  display: block;
  font-size: 16px;
  padding: 10px;
  margin-top: 20px;
  width: 100%;
}
</style>
